/*
 * date: 2009-11-20 
 * author: Maciej Smolenski <jezdnia@gmail.com>
 */
#ifndef _RPC_MONITOR_HPP_
#define _RPC_MONITOR_HPP_

#include "rpc_monitor_data.hpp"
#include "ctor_helper.hpp"
#include "rpc_share.hpp"
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/sync/interprocess_mutex.hpp>
#include <boost/interprocess/sync/scoped_lock.hpp>

namespace rpc_monitor
{

template<typename CT>
class rpc_monitor_service : rpc_share<CT>
{
	ctor_helper<rpc_monitor *, CT> m_monitor;
public:
	rpc_monitor_service(const char * p_memory_name, const int p_memory_size, const char *p_mutex_name);
	rpc_monitor_service(const char * p_memory_name, const char *p_mutex_name);
	~rpc_monitor_service();

	result_t get_services(stdset &so);
	result_t set_services(stdset &si);
};

template <typename CT>
rpc_monitor_service<CT>::rpc_monitor_service(const char * p_memory_name, const int p_memory_size, const char *p_mutex_name) : rpc_share<CT>(p_memory_name, p_memory_size, p_mutex_name), m_monitor(rpc_share<CT>::m_memory.m_t) {
	m_monitor.m_t->init(rpc_share<CT>::m_memory.m_t);
};

template <typename CT>
rpc_monitor_service<CT>::rpc_monitor_service(const char * p_memory_name, const char *p_mutex_name) : rpc_share<CT>(p_memory_name, p_mutex_name), m_monitor(rpc_share<CT>::m_memory.m_t) {
};

template <typename CT>
rpc_monitor_service<CT>::~rpc_monitor_service(){
};

template <>
rpc_monitor_service<bi::create_only_t>::~rpc_monitor_service(){
	m_monitor.m_t->deinit(rpc_share<bi::create_only_t>::m_memory.m_t);
};

template<typename CT>
result_t rpc_monitor_service<CT>::get_services(stdset &so)
{
	bi::managed_shared_memory &segment = rpc_share<CT>::m_memory.m_t;
	rpc_monitor &rpc = *m_monitor.m_t;
	bi::scoped_lock<bi::named_mutex> mutex(rpc_share<CT>::m_mutex.m_t);
	bi_set_to_std_set(segment, *rpc.m_services, so);
	return result_success;
}

template<typename CT>
result_t rpc_monitor_service<CT>::set_services(stdset &si)
{
	bi::managed_shared_memory &segment = rpc_share<CT>::m_memory.m_t;
	rpc_monitor &rpc = *m_monitor.m_t;
	bi::scoped_lock<bi::named_mutex> mutex(rpc_share<CT>::m_mutex.m_t);
	std_set_to_bi_set(segment, si, *rpc.m_services);
	return result_success;
}

}

#endif
